package com.taoyibao.hongcanting.http.interceptor;

import com.google.gson.Gson;
import com.taoyibao.hongcanting.http.ApiException;
import com.taoyibao.hongcanting.http.ErrorModel;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Response;

/**
 * @author TangAnna
 * @description: 通信失败拦截器
 * @date :${DATA} 13:19
 */
public class ApiExceptionIntercept implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {

        Response response = chain.proceed(chain.request());
        //如果httpCode 不是200 通信不成功 抛出API异常
        if (response.code() != 200) {
            ErrorModel error = new Gson().fromJson(response.body().string(), ErrorModel.class);
            throw new ApiException(error.code, error.msg);
        }
        return response;
    }
}
